
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Marwen
 */
public class AjouterTrader extends javax.swing.JFrame {

    /**
     * Creates new form AjouterTrader
     */
    public AjouterTrader() {
        initComponents();
        //jPanel3.setBorder(BorderFactory.createTitledBorder("Marwen is trying"));
        this.setTitle("Ajout Trader");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(400,100);
        this.setResizable(false);
       // System.out.println("color= "+this.getContentPane().getColorModel());    
        System.out.println("color= "+this.getContentPane().getForeground());    

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        valider = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        choixGrade = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        nom = new javax.swing.JTextField();
        password2 = new javax.swing.JPasswordField();
        prenom = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        poste = new javax.swing.JTextField();
        password1 = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        valider.setBackground(new java.awt.Color(255, 255, 255));
        valider.setText("valider");
        valider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validerActionPerformed(evt);
            }
        });

        choixGrade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Choisir un grade--", "Junior", "Senior", "Confirmé" }));

        jSeparator2.setBackground(new java.awt.Color(255, 51, 0));
        jSeparator2.setForeground(new java.awt.Color(0, 0, 0));

        jLabel6.setText("Mot de passe");

        jLabel1.setText("Nom");

        jLabel2.setText("Prénom");

        jSeparator1.setBackground(new java.awt.Color(251, 50, 0));
        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
        jSeparator1.setAlignmentY(2.5F);

        nom.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nomKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                nomKeyTyped(evt);
            }
        });

        password2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                password2KeyTyped(evt);
            }
        });

        prenom.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                prenomKeyTyped(evt);
            }
        });

        jLabel3.setText("Poste téléphone");

        jLabel7.setText("Confirmer mot de passe");

        poste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                posteActionPerformed(evt);
            }
        });
        poste.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                posteKeyTyped(evt);
            }
        });

        password1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                password1KeyTyped(evt);
            }
        });

        jLabel4.setText("Grade");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 560, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(38, Short.MAX_VALUE)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 560, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(nom, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(90, 90, 90))
                            .addComponent(prenom, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(poste, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(password1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(password2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(choixGrade, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(52, 52, 52))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(prenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(poste, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(password1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(password2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(choixGrade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Effacer");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(valider, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(169, 169, 169))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valider)
                    .addComponent(jButton1))
                .addGap(40, 40, 40))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void posteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_posteKeyTyped


        if (poste.getText().length() >= 12) {
            evt.consume();
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_posteKeyTyped

    private void posteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_posteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_posteActionPerformed

    private void prenomKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_prenomKeyTyped

        if (prenom.getText().length() >= 12) {
            evt.consume();// TODO add your handling code here:
        }//GEN-LAST:event_prenomKeyTyped
    }
        private void nomKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomKeyTyped

       if (nom.getText().length() >= 12) {
           evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_nomKeyTyped

    private void nomKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomKeyReleased


        // TODO add your handling code here:
  }//GEN-LAST:event_nomKeyReleased

    private void validerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validerActionPerformed



        String name = nom.getText().toUpperCase().replaceAll(" ", "");
        if (!name.matches(DbManip.regexLettreNumUnderscore)) {
            JOptionPane.showMessageDialog(null, "Vérifiez le nom", "Attention", JOptionPane.ERROR_MESSAGE);
            nom.setText("");
            return;
        }
        String firstname = prenom.getText().toUpperCase().replaceAll(" ", "");
        if (!firstname.matches(DbManip.regexLettreNumUnderscore)) {
            JOptionPane.showMessageDialog(null, "Vérifiez le prénom", "Attention", JOptionPane.ERROR_MESSAGE);
            prenom.setText("");
            return;
        }
        String post = poste.getText().toUpperCase().replaceAll(" ", "");
        //Verifier que  la chaîne ne contient que des chiffres
        if (post.matches(DbManip.regexDigit)) {
        }
        else {

            JOptionPane.showMessageDialog(null, "le numéro de poste doit être un entier", "Attention", JOptionPane.ERROR_MESSAGE);
            poste.setText("");
            return;
        }

        String pass1 = password1.getText();
        /*if (!pass1.matches(DbManip.regexLettreNumUnderscore)) {
            JOptionPane.showMessageDialog(null, "Verifier le password ! ", "Attention", JOptionPane.ERROR_MESSAGE);
            password1.setText("");
            password2.setText("");
            return;
        }
        * 
        */
        String pass2 = password2.getText();
        if (!pass1.equals(pass2)) {
            JOptionPane.showMessageDialog(null, "Le password n ' a pas été retapé correctement ! ", "Attention", JOptionPane.ERROR_MESSAGE);
            password1.setText("");
            password2.setText("");
            return;
        }
        pass1=DbManip.hash.encode(pass1);


        String grade = choixGrade.getSelectedItem().toString();
        int gradeTrader = 0;
        switch (grade) {

            case "Junior":
                gradeTrader = 1;
                break;
            case "Senior":
                gradeTrader = 2;
                break;
            case "Confirmé":
                gradeTrader = 3;
                break;
        }
        if (gradeTrader == 0) {
            JOptionPane.showMessageDialog(null, "Veuillez choisir  un grade", "Attention", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            if (!(DbManip.existDsTable("TRADER", "Nom_trader", name, "PRENOM_trader", firstname))) {

                if (JOptionPane.showConfirmDialog(null, "Voulez-vous continuer ?", "Confirmation ", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != 0) {
                    return;
                }
                // *********************** Requete D'insertion dans la table trader***************************  
                int matricule = DbManip.next_Id("trader", "MATRICULE");
                DbManip.requete = "insert into trader VALUES ('" + matricule + "','" + post + "', TIMESTAMP '" + DbManip.date + "','" + name + "','" + firstname + "')";
                DbManip.executerUpdate(DbManip.requete);
                //**********************************
                // *********************** Requete D'insertion dans la table trader grade***************************                      
                DbManip.requete = "insert into TRADER_GRADE VALUES ('" + matricule + "','" + gradeTrader + "',TIMESTAMP '" + DbManip.date + "')";
                DbManip.executerUpdate(DbManip.requete);
                // Requete d'insertion dans la table utilisateur
                String ch = firstname + " " + name;
                DbManip.requete = "insert into utilisateur values ('" + ch + "','" + pass1 + "','TRADER')";
                DbManip.executerUpdate(DbManip.requete);
                jButton1ActionPerformed(evt); // effacer tous les champs !
                JOptionPane.showMessageDialog(null, "Le trader " + firstname + " " + name + " a été ajouté avec succès", "Information", JOptionPane.INFORMATION_MESSAGE);

            }
            else {
                JOptionPane.showMessageDialog(null, "Ce nom d'utilisateur existe déja", "Erreur", JOptionPane.ERROR_MESSAGE);

            }
        }
        catch (SQLException ex) {
            Logger.getLogger(AjouterTrader.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_validerActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        nom.setText("");
        prenom.setText("");
        poste.setText("");
        password1.setText("");
        password2.setText("");
        choixGrade.setSelectedIndex(0);
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void password1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_password1KeyTyped

        if (password1.getText().length() >= 40) {
            evt.consume();// TODO add your handling code here:
        } 
        // TODO add your handling code here:
    }//GEN-LAST:event_password1KeyTyped

    private void password2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_password2KeyTyped
        if (password2.getText().length() >= 40) {
            evt.consume();// TODO add your handling code here:
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_password2KeyTyped

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox choixGrade;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField nom;
    private javax.swing.JPasswordField password1;
    private javax.swing.JPasswordField password2;
    private javax.swing.JTextField poste;
    private javax.swing.JTextField prenom;
    private javax.swing.JButton valider;
    // End of variables declaration//GEN-END:variables
}
